﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using HsTec.DigitalPlatform.Business;
using HsTec.DigitalPlatform.DataAccess;
using System.Reflection;
using HsTec.DigitalPlatform.Model.DTO;
using HsTec.DigitalPlatform.DataAccess.Search;

namespace HsTec.DigitalPlatform.WebSite.Controllers
{
    public class PlayerController : Controller
    {
        PlayerInfoManager playerinfomanager = new PlayerInfoManager();
        GradeInfoManager gradeinfomanager = new GradeInfoManager();
        SpecInfoManager specinfomanager = new SpecInfoManager();
        ClassInfoManager classinfomanager = new ClassInfoManager();
        //
        // GET: /Player/

        public ActionResult Index()
        {
            PlayerModel playermodel = new PlayerModel();
            IQueryable<GradeInfo> gradeinfolist = gradeinfomanager.FindALLGradeInfo();
            List<SelectListItem> _gradeinfoselect = new List<SelectListItem>();
            foreach (GradeInfo gradeinfo in gradeinfolist)
            {
                SelectListItem _deptselect = new SelectListItem();
                _deptselect.Text = gradeinfo.gradename;
                _deptselect.Value = gradeinfo.gradename;
                _gradeinfoselect.Add(_deptselect);
            }
            playermodel.gradelist = _gradeinfoselect;
            IQueryable<SpecInfo> specinfolist = specinfomanager.FindALLSpecInfo();
            List<SelectListItem> _specinfoselect = new List<SelectListItem>();
            foreach (SpecInfo sepcinfo in specinfolist)
            {
                SelectListItem _deptselect = new SelectListItem();
                _deptselect.Text = sepcinfo.spename;
                _deptselect.Value = sepcinfo.speid.ToString();
                _specinfoselect.Add(_deptselect);
            }
            playermodel.speclist = _specinfoselect;
            List<SelectListItem> _sexinfoselect = new List<SelectListItem>();
            SelectListItem _sex1 = new SelectListItem();
            _sex1.Text = "男";
            _sex1.Value = "男";
            SelectListItem _sex2 = new SelectListItem();
            _sex2.Text = "女";
            _sex2.Value = "女";
            _sexinfoselect.Add(_sex1);
            _sexinfoselect.Add(_sex2);
            playermodel.sexlist = _sexinfoselect;
            List<SelectListItem> classlist = new List<SelectListItem>();
            classlist.Add(new SelectListItem { Text = "请选择...", Value = "0" });
            playermodel.classlist = classlist;
            return View(playermodel);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetJsonGridData()
        {
            int page = int.Parse(Request.Form["page"]);
            int rp = int.Parse(Request.Form["rp"]);
            string sortname = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();
            PlayerSearchModel pageWhere = new PlayerSearchModel();
            try
            {
                pageWhere.sname = Request.Form["sname"].ToString();
                pageWhere.sno = Request.Form["sno"].ToString();
                pageWhere.sgrade = Request.Form["sgrade"].ToString();
                pageWhere.spec = Request.Form["spec"].ToString();
                pageWhere.sclass = Request.Form["sclass"].ToString();
            }
            catch
            {

            }

            List<ViPlayerInfo> viplayerinfos = playerinfomanager.PageInfo(page, rp, sortname, sortorder, pageWhere).ToList<ViPlayerInfo>();
            JsonFlexiGridData jsonflexgriddata = new JsonFlexiGridData();
            jsonflexgriddata.page = page;
            jsonflexgriddata.total = playerinfomanager.GetAllCount(pageWhere);
            if (jsonflexgriddata.rows == null)
            {
                jsonflexgriddata.rows = new List<FlexiGridRow>();
            }
            foreach (ViPlayerInfo viplayerinfo in viplayerinfos)
            {
                FlexiGridRow row = new FlexiGridRow()
                {
                    id = viplayerinfo.playerid.ToString(),
                    cell = GetPropertyList(viplayerinfo)
                };
                jsonflexgriddata.rows.Add(row);
            }
            return Json(jsonflexgriddata);
        }
        private List<string> GetPropertyList(object obj)
        {
            List<string> propertyList = new List<string>();

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                object o = property.GetValue(obj, null);
                propertyList.Add(o == null ? "" : o.ToString());
            }

            return propertyList;
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetClassInfo(string specid, string gradeid)
        {
            if (!String.IsNullOrEmpty(specid) && !String.IsNullOrEmpty(gradeid))
            {
                return Json(classinfomanager.FindClassInfoBySpecID(int.Parse(specid), int.Parse(gradeid)));
            }
            return null;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GenerationNo()
        {
            string sortorder = Request.Form["sortorder"].ToString();
            string sortname = Request.Form["sortname"].ToString();
            return Json(playerinfomanager.GenerationNo(sortorder, sortname));
        }
        [HttpGet]
        public ActionResult Add()
        {
            return PartialView();
        }
        [HttpPost]
        public ActionResult Add(FormCollection from)
        {
            PlayerInfo playerinfo = new PlayerInfo();
            try
            {
                playerinfo.playeno = from["playeno"];
                playerinfo.playername = from["playername"];
                playerinfo.password = from["password"];
                playerinfo.grade = from["grade"];
                playerinfo.specid = int.Parse(from["specid"]);
                playerinfo.classid = int.Parse(from["classid"]);
                playerinfo.sex = from["sex"];
                playerinfo.tel = from["tel"];
                playerinfo.playnumber = from["playnumber"];
                playerinfomanager.Add(playerinfo);
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "新增失败！");
            }
            return PartialView();
        }
        [HttpPost]
        public ActionResult Edit(FormCollection from)
        {
            try
            {
                PlayerInfo playerinfo = playerinfomanager.GetPlayerInfoByID(int.Parse(from["playerid"]));
                playerinfo.playeno = from["playeno"];
                playerinfo.playername = from["playername"];
                playerinfo.password = from["password"];
                playerinfo.grade = from["grade"];
                playerinfo.specid = int.Parse(from["specid"]);
                playerinfo.classid = int.Parse(from["classid"]);
                playerinfo.sex = from["sex"];
                playerinfo.tel = from["tel"];
                playerinfo.playnumber = from["playnumber"];
                playerinfomanager.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "修改失败！");
            }
            return PartialView();
        }
        [HttpPost]
        public JsonResult Delete(string ids)
        {
            try
            {
                string[] ary = ids.Split(',');
                foreach (string id in ary)
                {
                    PlayerInfo playerinfo = playerinfomanager.GetPlayerInfoByID(int.Parse(id));
                    playerinfomanager.Delete(playerinfo);
                    return Json(new { msg = "删除成功！" });
                }
            }
            catch
            {
                ModelState.AddModelError("", "删除失败！");
            }
            return Json(new { msg = "删除失败！" });
        }
        [HttpGet]
        public ActionResult View(int id)
        {
            return PartialView();
        }

        public void Export()
        {
            List<PlayerInfo> playerinfos = playerinfomanager.FindALLPlayerInfo().ToList<PlayerInfo>();

            HttpContext context = System.Web.HttpContext.Current;
            context.Response.Clear();
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            context.Response.AppendHeader("Content-Disposition", "attachment;filename=111.xls");
            context.Response.Write("1111111111111111");
            context.Response.End();
        }
    }
}
